package repository

import (
	"gitee.com/ling1314/origin-utility/db"
	"gitee.com/ling1314/origin-utility/model"
)

type mapHostSessionForAppRepository struct {
}

func (r mapHostSessionForAppRepository) Insert(m *model.MapHostSessionForApp) (err error) {
	err = db.GetDB().Create(m).Error
	return
}

func (r mapHostSessionForAppRepository) FindListBySessionIdsAndAppId(hostSessionIds []int64, appId int64) (li []model.MapHostSessionForApp, err error) {
	err = db.GetDB().Model(model.MapHostSessionForApp{}).Where("Host_session_id in ? and app_id = ?", hostSessionIds, appId).Find(&li).Error
	return
}

// SelectAppStartingTimesForActionSession 工具方法，检查主机上的活跃会话中是否启动过某个应用，尽量不要复用
func (r mapHostSessionForAppRepository) SelectAppStartingTimesForActionSession(hostId int64, appId int64) (num int64, err error) {
	err = db.GetDB().Raw(" select count(*) from va_map_host_session_app WHERE "+
		" host_session_id IN ( select host_session_id from va_host_session WHERE host_id =? AND status !='disconnected' )"+
		" AND app_id = ? )", hostId, appId).Error
	return
}
